/*
 * Title:        CloudScheduler Toolkit
 * Description:  a Toolkit for Modeling and Simulation of Job Scheduling and Resource Provisioning in Cloud System
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 * Author:       Fuhui Wu
 *
 * Copyright (c) 2013-2014, The National University of Defense Technology, China
 */

package org.nudt.jCloud.workload;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.nudt.jCloud.simulation.Request;


public class AnyJob extends LogicalJob{
	private int selectedJobIndex;
	private Random r;
	
	public AnyJob(double arrivalTime, Workload wl){
		super(arrivalTime, wl);
		r = new Random(System.currentTimeMillis());
		selectedJobIndex = -1;
	}

	@Override
	public boolean checkFinished() {
		return getJobs().get(selectedJobIndex).isFinished();
	}


	@Override
	public String type() {
		return "ANYJOB";
	}

	public int getSelectedJobIndex() {
		return selectedJobIndex;
	}

	public void setSelectedJobIndex(int selectedJobIndex) {
		this.selectedJobIndex = selectedJobIndex;
	}

	@Override
	public List<Request> request() {
		if(selectedJobIndex == -1){
			selectedJobIndex = r.nextInt(getJobs().size());
		}
		List<Request> requests = new ArrayList<Request>();
		if(getJobs().get(selectedJobIndex).getUnScheduledTasks().size()>0){
			Request request = new Request(this.getWl().getUser(), this.getJobs().get(selectedJobIndex),
					this.getJobs().get(selectedJobIndex).getUnScheduledTasks());
			requests.add(request);
		}
		return requests;
	}
	
}
